﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using kp.koordinator.contract;
using kp.plotter.contract;
using kp.portal.contract;

namespace kp.plotter
{

    public partial class Portal : Form, IPortal
    {
        public event Action<Berechnungsauftrag> OnBerechnungsauftrag;
        public event Action<ShowPlotCommand> OnShowPlotCommand;
        public event Action<WerteanzahlAnfrage> OnWerteanzahlAnfrage;


        public Portal(UserControl plotterUserControl)
        {
            InitializeComponent();

            plotterUserControl.Location = this.buttonPlotDummy.Location;
            plotterUserControl.Size = this.buttonPlotDummy.Size;
            plotterUserControl.Anchor = this.buttonPlotDummy.Anchor;
            this.Controls.Add( plotterUserControl);

            this.buttonPlotDummy.Visible = false;
        }


        private void buttonPlot_Click(object sender, EventArgs e)
        {
            double min = double.Parse(this.textBoxMin.Text);
            double max = double.Parse(this.textBoxMax.Text);

            int steps = 0;
            this.OnWerteanzahlAnfrage(new WerteanzahlAnfrage {processResult = r => steps = r});

            string formula = this.textBoxFormula.Text;

            PlotIt(formula, min, max, steps);
        }


        private void PlotIt(string formula, double min, double max, int steps)
        {
            IEnumerable<PointF> result = null;
            this.OnBerechnungsauftrag(new Berechnungsauftrag
                                             {
                                                 formel = formula,
                                                 xMin = min,
                                                 xMax = max,
                                                 werteanzahl = steps,

                                                 processResult = r => result = r
                                             });

            this.OnShowPlotCommand(new ShowPlotCommand {punkte = result});
        }
    }

}
